clear
set more off
timer clear 1
timer on 1
set seed 123
local programs "main reg_var dummies" 
foreach p of local programs {
capture program drop `p'
}

**************** 
***  PATHS   ***  
****************
*BCCR: 1 if at Central Bank
global BCCR=1
if $BCCR == 1 {
	global path "Y:/Projects/MNC_Suppliers/Suppliers_Alfaro-Urena_Manelici_Vasquez"
}

if $BCCR == 0 {
	global path "~/Documents/GitHub/Suppliers_Alfaro-Urena_Manelici_Vasquez"
}
cd $path 

*log file
if "${restrictions}"=="" & "${mnc_filename}"=="" & "${size_restriction}"=="" & "${sector_restriction}"=="" {
local logfile "${path0}/log_files/2-data_preparation"
set linesize 250
log using `logfile', replace
set linesize 250
}

/*
DATA PREPARATION BEFORE MAIN REGRESSION ANALYSIS
*/


********************** 
***  INPUT FILES   ***  
**********************
global events_data ""processed_data/events_data${restrictions}${mnc_filename}${size_restriction}${sector_restriction}.dta""
global location ""raw_data/firm_locations_from_workers.dta""

********************** 
***  OUTPUT FILES  ***  
**********************
global output ""processed_data/analysis_data${restrictions}${mnc_filename}${size_restriction}${sector_restriction}.dta""

******************* 
***  STRUCTURE  ***  
*******************

********************************************************************************
* main program: outline of the full code
********************************************************************************
prog main

*Creating variables for regressions
	quiet reg_var

*Creating event study dummies	
	quiet dummies
	
end

********************************************************************************
***  	Creating variables for regressions
********************************************************************************
prog reg_var

*loading processed data
use ${events_data}, clear

*variables in logs
capture drop IDs
sort ID year
gen y=log(ingresos)
gen l=log(trabaj)
gen k=log(total_activo_neto_)
gen m=log(costo_de)
gen va=log(va_)
egen IDs=group(ID)
egen province=group(provincia)
gen time=year-event
bys ID: egen min_t=min(time)
bys ID: egen max_t=max(time)
gen balance=(min_t<=-1 & max_t>=1)
gen s=y-l

*add cantones 
*location according to workers' data
preserve
use $location , clear
keep ID canton 
tostring ID , format(%24.0g) replace
tempfile location
save `location', replace 
restore
merge m:1 ID using `location'
drop if _m==2
drop _m 

* number of clients
replace tot_trans_mnc=0 if tot_trans_mnc==. 
* sales to non-MNC clients
gen dom_sales=ingresosir-tot_trans_mnc if year>=2008
gen l_dom_sales= log(dom_sales)
* sales to clients other than the MNCs triggering the event
gen sales_not_MNC= ingresosir-trans_mncs_event
gen l_not_MNC_event=log(sales_not_MNC)
* domestic clients
replace n_fore_cli=0 if n_fore_cli==. 
gen l_dom_clients=log(n_clients-n_fore_cli+1)
gen l_trans=log(all_trans)
* sales to clients other than the MNCs triggering the event
gen sales_not_MNC_2= all_trans-trans_mncs_event 
gen l_not_MNC_event_2=log(sales_not_MNC_2)
* clients besides MNC triggering event
gen n_clients_non_event=n_clients-n_trig_MNC+1
gen l_clients_non_event=log(n_clients_non_event)
gen l_ave2=l_not_MNC_event_2-l_clients_non_event
* not evented
gen sales_not_evented_or_mnc=ingresosir_ - trans_mncs_event - trans_evented
gen l_not_evented=log(sales_not_evented_or_mnc)
* sales to not suppliers
gen sales_not_supp=ingresosir_ - trans_mncs_event - trans_to_suppliers
gen l_not_supp=log(sales_not_supp)

* EXPORT-IMPORTS
replace imports=0 if imports==.
replace exports=0 if exports==.
gen exporter=(exports>0 & exports!=.)
gen importer=(imports>0 & imports!=.) 
replace purch=0 if purch==. 
gen imp_sh=imports/(imports+purch)

*saving temp file
save temp/data1.dta, replace

end 
	
********************************************************************************
***  	DUMMIES FOR EVENT STUDY
********************************************************************************
prog dummies 

*using previous data
use temp/data1.dta, clear

*limits 
local lower_lim=-5
local upper_lim=5
capture drop D_*
scalar first_year=`lower_lim'
scalar last_year=`upper_lim'
local dummies ""

forvalue i=`=first_year'(1)`=last_year'{
if `i'<0{
scalar j=abs(`i')
gen D_m`=j' =( time==`i')
label var D_m`=j' "t $ =`i' $"
if `i'==`=first_year'{
replace D_m`=j' =( time<=`i' & time!=.)
label var D_m`=j' "t $ \leq `i'$"
}
}
if `i'>=0{
gen D_`i' = (time==`i')
label var D_`i' "t $ =`i'$"
if `i'==`=last_year'{
replace D_`i' = (time>=`i'  & time!=.)
label var D_`i' "t $ \geq `i'$"
}    
}
}
drop D_m1
gen time_reg=time
replace time_reg=`upper_lim' if time>=`upper_lim' & time!=.
replace time_reg=`lower_lim' if time<=`lower_lim'

*saving main output
save $output, replace 

end


******************************************************************************** 
***  EXECUTE THE PROGRAM main   
********************************************************************************
main

******************************************************************************** 
*** ERASING TEMP DATA
********************************************************************************
capture erase temp/data1.dta

******************************************************************************** 
***  TIMER
********************************************************************************
if "${restrictions}"=="" & "${mnc_filename}"=="" & "${size_restriction}"=="" & "${sector_restriction}"=="" {
timer off 1
*time in seconds
timer list 1
*time in minutes
local time_ =round(`r(t1)'/60)
display "the code takes `time_' minutes in total"

log close
translate `logfile'.smcl `logfile'.txt , replace linesize(150)
capture erase `logfile'.smcl 
}
